/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgregarEntradaMuro.java
 *
 * Created on 02-sep-2011, 10:27:07
 */
package Ventanas;

import Objetos.PropiedadesIP;
import Proxies.CtrlAgregarEntradaMuroWS;
import Proxies.CtrlAgregarEntradaMuroWSService;
import Proxies.CtrlDetalleDeGrupoWS;
import Proxies.CtrlDetalleDeGrupoWSService;
import Proxies.CtrlUsuariosWS;
import Proxies.CtrlUsuariosWSService;
import Proxies.DataUsuario;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.xml.namespace.QName;

/**
 *
 * @author jota
 */
public class AgregarEntradaMuro extends javax.swing.JFrame {

    /** Creates new form AgregarEntradaMuro */
    private CtrlAgregarEntradaMuroWS ctrlMuro;

    public AgregarEntradaMuro() {
        initComponents();

        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();

        URL dirMuro = null;
        try {
            dirMuro = new URL(IP + "CtrlAgregarEntradaMuro?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(AgregarEntradaMuro.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaMuro = new QName("http://WebServices/", "CtrlAgregarEntradaMuroWSService");

        ctrlMuro = new CtrlAgregarEntradaMuroWSService(dirMuro, qNaMuro).getCtrlAgregarEntradaMuroWSPort();

        buttonGroup_TipoMuro.add(jRadioButton_Grupo);
        buttonGroup_TipoMuro.add(jRadioButton_Usuarios);

        jTable_GrupUsuarios.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        switch (jComboBox_TipoEntrada.getSelectedIndex()) {
            case 0:
                //Texto
                jTextArea_Cuerpo.setEnabled(true);
                jTextField_Img.setEnabled(false);
                jTextField_Img.setBackground(Color.lightGray);
                break;
            case 1:
                //Imagen
                jTextArea_Cuerpo.setEnabled(false);
                jTextField_Img.setEnabled(true);
                jTextArea_Cuerpo.setBackground(Color.lightGray);
                break;
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup_TipoMuro = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jComboBox_TipoEntrada = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jTextField_NomEntrada = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_Cuerpo = new javax.swing.JTextArea();
        jTextField_Img = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField_Buscar = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable_GrupUsuarios = new javax.swing.JTable();
        jRadioButton_Grupo = new javax.swing.JRadioButton();
        jRadioButton_Usuarios = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable_MiembrosGrupo = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jButton_Confirmar = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();

        jLabel2.setText("Seleccione tipo de entrada al muro:");

        jComboBox_TipoEntrada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Texto", "Imagen" }));
        jComboBox_TipoEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_TipoEntradaActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel10.setText("RED - Agregar Entrada a Muro");

        jLabel3.setText("Título de la entrada:");

        jTextArea_Cuerpo.setColumns(20);
        jTextArea_Cuerpo.setRows(5);
        jTextArea_Cuerpo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextArea_CuerpoKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea_Cuerpo);

        jLabel4.setText("Buscar");

        jTextField_Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_BuscarActionPerformed(evt);
            }
        });
        jTextField_Buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_BuscarKeyReleased(evt);
            }
        });

        jTable_GrupUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable_GrupUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_GrupUsuariosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable_GrupUsuarios);

        jRadioButton_Grupo.setText("Grupos");
        jRadioButton_Grupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_GrupoActionPerformed(evt);
            }
        });
        jRadioButton_Grupo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jRadioButton_GrupoFocusGained(evt);
            }
        });

        jRadioButton_Usuarios.setText("Usuarios");
        jRadioButton_Usuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_UsuariosActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel1.setText("Cuerpo del texto");

        jLabel5.setText("URL imagen");

        jTable_MiembrosGrupo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Mail", "Nombre"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable_MiembrosGrupo.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(jTable_MiembrosGrupo);
        jTable_MiembrosGrupo.getColumnModel().getColumn(0).setResizable(false);
        jTable_MiembrosGrupo.getColumnModel().getColumn(1).setResizable(false);
        jTable_MiembrosGrupo.getColumnModel().getColumn(2).setResizable(false);

        jLabel7.setText("Autor del contenido (Solo en caso que el muro sea de Grupo)");

        jButton_Confirmar.setText("Confirmar");
        jButton_Confirmar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_ConfirmarMouseClicked(evt);
            }
        });
        jButton_Confirmar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton_ConfirmarKeyPressed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jLabel10))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jLabel2)
                        .addGap(41, 41, 41)
                        .addComponent(jComboBox_TipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(110, 110, 110)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(81, 81, 81)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jTextField_Img, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(100, 100, 100)
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(jTextField_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(89, 89, 89)
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
                                        .addComponent(jLabel7)
                                        .addGap(50, 50, 50))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(23, 23, 23)
                                .addComponent(jTextField_NomEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jRadioButton_Grupo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton_Usuarios)))
                .addContainerGap(14, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(369, Short.MAX_VALUE)
                .addComponent(jButton_Cancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_Confirmar)
                .addGap(235, 235, 235))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3))
                    .addComponent(jTextField_NomEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox_TipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_Img, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton_Grupo)
                    .addComponent(jRadioButton_Usuarios))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Confirmar)
                    .addComponent(jButton_Cancelar))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox_TipoEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_TipoEntradaActionPerformed
        // TODO add your handling code here:

        switch (jComboBox_TipoEntrada.getSelectedIndex()) {
            case 0:
                //Texto
                jTextArea_Cuerpo.setEnabled(true);
                jTextField_Img.setEnabled(false);
                jTextArea_Cuerpo.setBackground(jTextField_Img.getBackground());
                jTextField_Img.setBackground(Color.lightGray);
                break;
            case 1:
                //Imagen
                jTextArea_Cuerpo.setEnabled(false);
                jTextField_Img.setEnabled(true);
                jTextField_Img.setBackground(jTextArea_Cuerpo.getBackground());
                jTextArea_Cuerpo.setBackground(Color.lightGray);
                break;
        }
    }//GEN-LAST:event_jComboBox_TipoEntradaActionPerformed

    private void jTextField_BuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_BuscarKeyReleased
        // TODO add your handling code here:

        setSort();
        if (jTable_GrupUsuarios.getSelectedRowCount() == 0
                && jTable_GrupUsuarios.getRowCount() > 0) {
            jTable_GrupUsuarios.setRowSelectionInterval(0, 0);
        }

    }//GEN-LAST:event_jTextField_BuscarKeyReleased

    private void jRadioButton_GrupoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jRadioButton_GrupoFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton_GrupoFocusGained

    private void jRadioButton_GrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_GrupoActionPerformed
        // TODO add your handling code here:
        actualizarTablaGrupos();
        jTable_MiembrosGrupo.setEnabled(true);

    }//GEN-LAST:event_jRadioButton_GrupoActionPerformed

    private void jRadioButton_UsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_UsuariosActionPerformed
        // TODO add your handling code here:
        actualizarTablaUsuarios();
        jTable_MiembrosGrupo.setEnabled(false);

        DefaultTableModel model = new DefaultTableModel();
        jTable_MiembrosGrupo.setModel(model);
    }//GEN-LAST:event_jRadioButton_UsuariosActionPerformed

    private void jTextArea_CuerpoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea_CuerpoKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextArea_CuerpoKeyReleased

    private void jTextField_BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_BuscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField_BuscarActionPerformed

    private void jTable_GrupUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_GrupUsuariosMouseClicked
        // TODO add your handling code here:

        actualizarMiembrosGrupo();

    }//GEN-LAST:event_jTable_GrupUsuariosMouseClicked

    private void jButton_ConfirmarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton_ConfirmarKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton_ConfirmarKeyPressed

    private void jButton_ConfirmarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_ConfirmarMouseClicked
        // TODO add your handling code here:.

        boolean error = false;
        String errMens = "ERROR: ";

        //Control de errores.
        if (jTextField_NomEntrada.getText().equals("")) {
            error = true;
            errMens += "\nDebe indicar nombre del contenido.";
        }
        if (jComboBox_TipoEntrada.getSelectedIndex() == 0) {
            //Texto.
            if (jTextArea_Cuerpo.getText().isEmpty()) {
                error = true;
                errMens += "\nDebe indicar cuerpo del texto a ingresar en el muro.";
            }
        }
        if (jComboBox_TipoEntrada.getSelectedIndex() == 1) {
            //Imagen.
            if (jTextField_Img.getText().isEmpty()) {
                error = true;
                errMens += "\nDebe indicar imagen a ingresar en el muro.";
            }
        }
        if (jRadioButton_Grupo.isSelected()) {
            if (jTable_MiembrosGrupo.getSelectedRowCount() == 0) {
                error = true;
                errMens += "\nEl muro pertence a un grupo. Seleccione el autor del contenido.";
            }
        }
        if (!jRadioButton_Grupo.isSelected()
                && !jRadioButton_Usuarios.isSelected()) {
            error = true;
            errMens += "\nSeleccione el tipo de muro en donde se va a ingresar el contenido.";
        }

        if (!error) {


            String nomContenido, cuerpoTexto, urlmagen, nomGrupo, correoUsr, correoAutor, tipoContenido;

            nomContenido = jTextField_NomEntrada.getText();
            cuerpoTexto = jTextArea_Cuerpo.getText();
            urlmagen = jTextField_Img.getText();
            tipoContenido = jComboBox_TipoEntrada.getSelectedItem().toString();


            if (jRadioButton_Usuarios.isSelected()) {

                correoUsr = jTable_GrupUsuarios.getValueAt(jTable_GrupUsuarios.getSelectedRow(), 2).toString();

                if (jComboBox_TipoEntrada.getSelectedIndex() == 0) {

                    ctrlMuro.agregarContTexto(nomContenido, cuerpoTexto, correoUsr, correoUsr, tipoContenido, true);
                } else {
                    ctrlMuro.agregarContImagen(nomContenido, urlmagen, correoUsr, correoUsr, tipoContenido, true);
                }
                JOptionPane.showMessageDialog(null, "Se agregó el contenido en el muro del usuario");
            } else {

                nomGrupo = jTable_GrupUsuarios.getValueAt(jTable_GrupUsuarios.getSelectedRow(), 0).toString();
                correoAutor = jTable_MiembrosGrupo.getValueAt(jTable_MiembrosGrupo.getSelectedRow(), 2).toString();

                if (jComboBox_TipoEntrada.getSelectedIndex() == 0) {

                    ctrlMuro.agregarContTexto(nomContenido, cuerpoTexto, nomGrupo, correoAutor, tipoContenido, true);
                } else {
                    ctrlMuro.agregarContImagen(nomContenido, urlmagen, nomGrupo, correoAutor, tipoContenido, true);
                }

                JOptionPane.showMessageDialog(null, "Se agregó el contenido en el muro del Grupo");
            }


        } else {
            JOptionPane.showMessageDialog(null, errMens, "ERROR", JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_jButton_ConfirmarMouseClicked
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AgregarEntradaMuro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AgregarEntradaMuro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AgregarEntradaMuro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AgregarEntradaMuro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AgregarEntradaMuro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup_TipoMuro;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Confirmar;
    private javax.swing.JComboBox jComboBox_TipoEntrada;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JRadioButton jRadioButton_Grupo;
    private javax.swing.JRadioButton jRadioButton_Usuarios;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable_GrupUsuarios;
    private javax.swing.JTable jTable_MiembrosGrupo;
    private javax.swing.JTextArea jTextArea_Cuerpo;
    private javax.swing.JTextField jTextField_Buscar;
    private javax.swing.JTextField jTextField_Img;
    private javax.swing.JTextField jTextField_NomEntrada;
    // End of variables declaration//GEN-END:variables

    private void actualizarTablaUsuarios() {

        Object[] fila = new Object[3];

        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();

        URL dirUsr = null;
        try {
            dirUsr = new URL(IP + "CtrlUsuarios?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(AgregarEntradaMuro.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaUsr = new QName("http://WebServices/", "CtrlUsuariosWSService");
        CtrlUsuariosWS ctrlUsr = new CtrlUsuariosWSService(dirUsr, qNaUsr).getCtrlUsuariosWSPort();

        Iterator<Proxies.DataUsuario> itera = ctrlUsr.listaUsuarios().getItem().iterator();
        Proxies.DataUsuario usr;
        DefaultTableModel model = new DefaultTableModel();
        jTable_GrupUsuarios.setModel(model);
        setSort();

        model.addColumn("Nombre");
        model.addColumn("Nick");
        model.addColumn("Correo");

        while (itera.hasNext()) {
            usr = itera.next();
            fila[0] = usr.getNombre();
            fila[1] = usr.getNick();
            fila[2] = usr.getCorreo();
            model.addRow(fila);
            jTable_GrupUsuarios.setRowSelectionInterval(0, 0);

        }

    }

    private void actualizarTablaGrupos() {

        Object[] fila = new Object[1];
        Iterator<Proxies.DataGrupo> itera = ctrlMuro.listarGrupos().getItem().iterator();
        Proxies.DataGrupo grp;

        DefaultTableModel model = new DefaultTableModel();
        jTable_GrupUsuarios.setModel(model);
        setSort();
        model.addColumn("Nombre");
        model.setRowCount(0);
        model.setColumnCount(1);
        while (itera.hasNext()) {
            grp = itera.next();
            fila[0] = grp.getNombre();
            model.addRow(fila);
            jTable_GrupUsuarios.setRowSelectionInterval(0, 0);
        }

        actualizarMiembrosGrupo();



    }

    private void setSort() {

        TableModel modelo = jTable_GrupUsuarios.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(modelo);
        jTable_GrupUsuarios.setRowSorter(sorter);
        String text = jTextField_Buscar.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }

    }

    private void actualizarMiembrosGrupo() {
        if ((jTable_GrupUsuarios.getSelectedRowCount() > 0)
                && (jRadioButton_Grupo.isSelected())) {

            String nomGrupo = jTable_GrupUsuarios.getModel().getValueAt(
                    jTable_GrupUsuarios.getSelectedRow(), 0).toString();

            Object[] fila = new Object[3];
            DefaultTableModel model = new DefaultTableModel();
            jTable_MiembrosGrupo.setModel(model);

            model.addColumn("Nombre");
            model.addColumn("Nick");
            model.addColumn("Correo");

            PropiedadesIP props = PropiedadesIP.getInstance();
            String IP = props.getServidorCentralIP();

            URL dirGr = null;
            try {
                dirGr = new URL(IP + "CtrlDetalleGrupo?wsdl");
            } catch (MalformedURLException ex) {
                Logger.getLogger(AgregarEntradaMuro.class.getName()).log(Level.SEVERE, null, ex);
            }
            QName qNaGr = new QName("http://WebServices/", "CtrlDetalleDeGrupoWSService");
            CtrlDetalleDeGrupoWS ctrlDetalles = new CtrlDetalleDeGrupoWSService(dirGr, qNaGr).getCtrlDetalleDeGrupoWSPort();

            URL dirUsr = null;
            try {
                dirUsr = new URL(IP + "CtrlUsuarios?wsdl");
            } catch (MalformedURLException ex) {
                Logger.getLogger(AgregarEntradaMuro.class.getName()).log(Level.SEVERE, null, ex);
            }
            QName qNaUsr = new QName("http://WebServices/", "CtrlUsuariosWSService");
            CtrlUsuariosWS ctrlUsr = new CtrlUsuariosWSService(dirUsr, qNaUsr).getCtrlUsuariosWSPort();


            Proxies.DataGrupo dg = ctrlDetalles.detallarGrupo(nomGrupo);
            String creador = dg.getCreador();

            DataUsuario dUsr = ctrlUsr.getUsuario(creador);

            fila[0] = dUsr.getNombre();
            fila[1] = dUsr.getNick();
            fila[2] = dUsr.getCorreo();
            model.addRow(fila);

            Iterator<String> itera = dg.getModeradores().iterator();
            DataUsuario moderador = null;

            while (itera.hasNext()) {

                moderador = ctrlUsr.getUsuario(itera.next());
                fila[0] = moderador.getNombre();
                fila[1] = moderador.getNick();
                fila[2] = moderador.getCorreo();
                model.addRow(fila);
                jTable_MiembrosGrupo.setRowSelectionInterval(0, 0);

            }

        }

    }
}
